{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7ea5292c-e206-4433-bdf9-d752515514be",
   "metadata": {},
   "source": [
    "### 以在mac上运行BioMedGPT-LM-7B为例"
   ]
  },
  {
   "cell_type": "raw",
   "id": "6dce2078-793c-42b9-b0e6-05f0ee5fa5bb",
   "metadata": {},
   "source": [
    "# 在终端创建并激活环境\n",
    "conda create -n biomedgpt python=3.10\n",
    "conda activate biomedgpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "291ba1ca-10fe-43c8-82d8-8e58e1318277",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: torch in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (2.0.1)\n",
      "Requirement already satisfied: torchvision in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (0.15.2)\n",
      "Requirement already satisfied: torchaudio in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (2.0.2)\n",
      "Requirement already satisfied: filelock in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from torch) (3.12.2)\n",
      "Requirement already satisfied: typing-extensions in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from torch) (4.7.1)\n",
      "Requirement already satisfied: sympy in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from torch) (1.12)\n",
      "Requirement already satisfied: networkx in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from torch) (3.1)\n",
      "Requirement already satisfied: jinja2 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from torch) (3.1.2)\n",
      "Requirement already satisfied: numpy in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from torchvision) (1.25.2)\n",
      "Requirement already satisfied: requests in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from torchvision) (2.31.0)\n",
      "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from torchvision) (10.0.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from jinja2->torch) (2.1.3)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from requests->torchvision) (3.2.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from requests->torchvision) (3.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from requests->torchvision) (2.0.4)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from requests->torchvision) (2023.7.22)\n",
      "Requirement already satisfied: mpmath>=0.19 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from sympy->torch) (1.3.0)\n",
      "Requirement already satisfied: transformers in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (4.32.0)\n",
      "Requirement already satisfied: filelock in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from transformers) (3.12.2)\n",
      "Requirement already satisfied: huggingface-hub<1.0,>=0.15.1 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from transformers) (0.16.4)\n",
      "Requirement already satisfied: numpy>=1.17 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from transformers) (1.25.2)\n",
      "Requirement already satisfied: packaging>=20.0 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from transformers) (23.1)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from transformers) (6.0.1)\n",
      "Requirement already satisfied: regex!=2019.12.17 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from transformers) (2023.8.8)\n",
      "Requirement already satisfied: requests in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from transformers) (2.31.0)\n",
      "Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from transformers) (0.13.3)\n",
      "Requirement already satisfied: safetensors>=0.3.1 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from transformers) (0.3.3)\n",
      "Requirement already satisfied: tqdm>=4.27 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from transformers) (4.66.1)\n",
      "Requirement already satisfied: fsspec in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from huggingface-hub<1.0,>=0.15.1->transformers) (2023.6.0)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from huggingface-hub<1.0,>=0.15.1->transformers) (4.7.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from requests->transformers) (3.2.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from requests->transformers) (3.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from requests->transformers) (2.0.4)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages (from requests->transformers) (2023.7.22)\n"
     ]
    }
   ],
   "source": [
    "# 安装依赖\n",
    "!pip install torch torchvision torchaudio\n",
    "!pip install transformers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ab6f1cd4-16c6-4731-9c9b-e45c6d60ca90",
   "metadata": {},
   "outputs": [],
   "source": [
    "import transformers  # 导入transformers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "48de67b2-6e9f-4c91-a744-75a13cf68e65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "73838521abdc404ab5b2c7053f91208f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/3 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages/transformers/generation/configuration_utils.py:362: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.9` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`. This was detected when initializing the generation config instance, which means the corresponding file may hold incorrect parameterization and should be fixed.\n",
      "  warnings.warn(\n",
      "/Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages/transformers/generation/configuration_utils.py:367: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.6` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`. This was detected when initializing the generation config instance, which means the corresponding file may hold incorrect parameterization and should be fixed.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "# 加载模型\n",
    "model_path = '/Users/a/BioMedGPT-LM-7B'  # 模型和tokenizer文件存放路径\n",
    "model = transformers.AutoModelForCausalLM.from_pretrained(model_path)\n",
    "#print(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7babf3bc-6ded-4970-9fb6-85376192d048",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LlamaConfig {\n",
       "  \"_name_or_path\": \"/Users/a/BioMedGPT-LM-7B\",\n",
       "  \"architectures\": [\n",
       "    \"LlamaForCausalLM\"\n",
       "  ],\n",
       "  \"bos_token_id\": 1,\n",
       "  \"eos_token_id\": 2,\n",
       "  \"hidden_act\": \"silu\",\n",
       "  \"hidden_size\": 4096,\n",
       "  \"initializer_range\": 0.02,\n",
       "  \"intermediate_size\": 11008,\n",
       "  \"max_length\": 4096,\n",
       "  \"max_position_embeddings\": 4096,\n",
       "  \"model_type\": \"llama\",\n",
       "  \"num_attention_heads\": 32,\n",
       "  \"num_hidden_layers\": 32,\n",
       "  \"num_key_value_heads\": 32,\n",
       "  \"pad_token_id\": 0,\n",
       "  \"pretraining_tp\": 1,\n",
       "  \"rms_norm_eps\": 1e-06,\n",
       "  \"rope_scaling\": null,\n",
       "  \"tie_word_embeddings\": false,\n",
       "  \"torch_dtype\": \"float32\",\n",
       "  \"transformers_version\": \"4.32.0\",\n",
       "  \"use_cache\": false,\n",
       "  \"vocab_size\": 32000\n",
       "}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3976c018-ab5b-4937-9f84-9f3f59ded81d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载模型对应的tokenizer\n",
    "tokenizer = transformers.AutoTokenizer.from_pretrained(model_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c83798aa-daaf-44e5-8848-75ce97d6de66",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = [\"What's the function of Aspirin?\"]\n",
    "# 使用tokenizer处理文本\n",
    "input = tokenizer(text,\n",
    "              truncation=True,\n",
    "              return_tensors=\"pt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "62e42d90-e5de-468e-a2ee-9cc239b2f9af",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input_ids': tensor([[    1,  1724, 29915, 29879,   278,   740,   310, 26562,   381,   262,\n",
       "         29973]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2fc03431-9b1a-404f-82c7-091918f39fd9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/a/opt/anaconda3/envs/biomedgpt/lib/python3.10/site-packages/transformers/generation/configuration_utils.py:399: UserWarning: `num_beams` is set to 1. However, `early_stopping` is set to `True` -- this flag is only used in beam-based generation modes. You should set `num_beams>1` or unset `early_stopping`.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "output = model.generate(inputs=input.input_ids, max_new_tokens=128, early_stopping=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e948f227-ea3b-42ce-8d66-f21450035a68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"<s> What's the function of Aspirin?\\nAspirin is a non-steroidal anti-inflammatory drug (NSAID) that is widely used for the treatment of pain, fever, and inflammation. It is also used to prevent heart attacks, strokes, and blood clots. Aspirin works by inhibiting the enzyme cyclooxygenase (COX), which is involved in the production of prostaglandins, a group of hormone-like substances that play a role in pain, inflammation, and fever. By inhibiting COX, aspirin\""
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 解码成文字\n",
    "tokenizer.decode(output[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2af8a41-a473-45cf-9836-c2c8a22be2de",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
